﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shop.Core.Properties;
using Shop.Infrastructure.Containers;
using Shop.Core.Events;
using Microsoft.Practices.Unity;
using Shop.Core.Domain.Customers;
using Shop.Core.Domain.Common;

namespace Shop.Core.Domain.Orders
{
    public abstract class BaseFlowNode : IOrderFlowNode
    {
        public virtual bool FlowToNext(Order order)
        {
            order.SetFlowNode(FlowNodeFactory.GetFlowNode(order.OrderStatus));
            return true;
        }

        public virtual void Cancel(Order order)
        {
            if (!this.CanCancel(order))
            {
                throw new InvalidOperationException(string.Format(BizResource.Order_Cancel_InvaildOp, order.Id));
            }
            order.ShippingStatus = Shipping.ShippingStatus.Cancelled;
            this.FlowToNext(order);
        }

        public abstract OrderStatus StatusCode
        {
            get;
        }

        public abstract bool CanPay(Order order);

        public virtual void Pay(Order order,Money money)
        {
            order.Paid(money);
        }

        public abstract bool CanEdit(Order order);

        /// <summary>
        /// 开始编辑订单
        /// </summary>
        /// <param name="order"></param>
        /// <exception cref="System.InvalidOperationException">
        /// 
        /// </exception>
        public virtual void BeginEdit(Order order)
        {
            if (!this.CanEdit(order))
            {
                throw new InvalidOperationException(string.Format(BizResource.Order_BeginEdit_InvalidOp, order.Id));
            }
            order.ProduceLockStatus = ProduceLockStatus.CustomerEditting;
            this.FlowToNext(order);
        }

        /// <summary>
        /// 默认不能编辑
        /// </summary>
        /// <param name="order"></param>
        /// <exception cref="System.InvalidOperationException">
        /// 
        /// </exception>
        public virtual void EndEdit(Order order)
        {
            throw new InvalidOperationException(
                string.Format(BizResource.Order_EndEdit_InvalidOp, order.Id));
        }

        public abstract bool CanCancel(Order order);
    }
}
